package com.youan.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youan.pangu.base.bean.ElementTree;
import com.youan.pangu.base.exception.BusinessException;
import com.youan.serve.dao.domain.Function;
import com.youan.serve.dao.idao.FunctionMapper;
import com.youan.serve.enums.EnumFunType;
import com.youan.serve.service.IFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author auto_code
 * @since 2020-03-04
 */
@Service
public class FunctionServiceImpl extends ServiceImpl<FunctionMapper, Function> implements IFunctionService {

    @Autowired
    private FunctionMapper functionMapper;

    @Override
    public List<ElementTree> getFunctionsElementTree() {
        // 先获取所有Router
        List<Function> functionList = functionMapper.selectList(new QueryWrapper<Function>().eq("fun_type", "page").orderByAsc("fun_seqno"));
        // 组成ElementTree 将父节点为空的组成ElementTree，他们作为根目录
        List<ElementTree> elementTreeList = new ArrayList<ElementTree>();
        // 循环构建ElementTree集合
        for(int i=0; i<functionList.size(); i++) {
            Function ElementTree =  functionList.get(i);
            // 构建前台节点id和label
            ElementTree.setId();
            ElementTree.setLabel();
            // 如果父节点是空，证明是根节点，添加到ElementTreeList
            if(StringUtils.isEmpty(ElementTree.getParentId())) {
                elementTreeList.add(ElementTree);
                packingChildElementTreeList(ElementTree,functionList,ElementTree.getFunId());
            }
        }
        // 将子节点转载入父节点
        return elementTreeList;
    }

    @Override
    public List<ElementTree> getFunctionsElementTreeByUserRoles(List<String> roleIdList) {

        // 先获取所有Router
        List<Function> functionList = functionMapper.getFunctionsByUserRoles(roleIdList);
        // 组成ElementTree 将父节点为空的组成ElementTree，他们作为根目录
        List<ElementTree> elementTreeList = new ArrayList<ElementTree>();
        // 循环构建ElementTree集合
        for(int i=0; i<functionList.size(); i++) {
            Function ElementTree =  functionList.get(i);
            // 构建前台节点id和label
            ElementTree.setId();
            ElementTree.setLabel();
            // 如果父节点是空，证明是根节点，添加到ElementTreeList
            if(StringUtils.isEmpty(ElementTree.getParentId())) {
                elementTreeList.add(ElementTree);
                packingChildElementTreeList(ElementTree,functionList,ElementTree.getFunId());
            }
        }
        // 将子节点转载入父节点
        return elementTreeList;
    }

    @Override
    public Boolean validateFun(Function function) throws BusinessException {
        if (null == function) {
            throw new BusinessException("菜单信息为空");
        }
        if(StringUtils.isEmpty(function.getFunType())){
            throw new BusinessException("类型为空");
        }
        if(EnumFunType.page.getCode().equals(function.getFunType())) {
            if(StringUtils.isEmpty(function.getFunName())){
                throw new BusinessException("菜单名称为空");
            }
            if(StringUtils.isEmpty(function.getFunPath())){
                throw new BusinessException("菜单路由路径为空");
            }
            if(StringUtils.isEmpty(function.getFunAbsPath())){
                throw new BusinessException("菜单绝对路径为空");
            }
            if(StringUtils.isEmpty(function.getFunContent())){
                throw new BusinessException("菜单组件路径为空");
            }
            if(StringUtils.isEmpty(function.getParentId())){
                throw new BusinessException("父节点为空");
            }
        }else if(EnumFunType.button.getCode().equals(function.getFunType())) {
            if(StringUtils.isEmpty(function.getFunName())){
                throw new BusinessException("按钮名称为空");
            }
            if(StringUtils.isEmpty(function.getParentId())){
                throw new BusinessException("父节点为空");
            }
        }else {
            throw new BusinessException("不存在菜单类型："+function.getFunType());
        }
        return true;
    }

    /**
     * 递归调用，设置child
     * @param ElementTree
     * @param functionList
     * @param parentid
     */
    private void packingChildElementTreeList(ElementTree ElementTree, List<Function> functionList, String parentid) {
        if(!isParentRoot(functionList,parentid)) {
            return;
        }
        List<ElementTree> ElementTreeList = new ArrayList<ElementTree>();
        for(int i=0; i<functionList.size(); i++) {
            Function subElementTree =  functionList.get(i);
            if((!StringUtils.isEmpty(subElementTree.getParentId()))&&subElementTree.getParentId().equals(parentid)) {
                ElementTreeList.add(subElementTree);
                packingChildElementTreeList(subElementTree,functionList,subElementTree.getFunId());
            }
        }
        ElementTree.setChildren(ElementTreeList);
    }

    /**
     * 判断该节点是否是父节点
     * @return
     */
    private Boolean isParentRoot(List<Function> routerList,String parentId) {
        for(int i=0; i<routerList.size(); i++) {
            if((!StringUtils.isEmpty(routerList.get(i).getParentId()))&&routerList.get(i).getParentId().equals(parentId)) {
                return true;
            }
        }
        return false;
    }
}
